/*
* web call 所需的js
* 需要jquery.js,swfobject.js,VideoPhone11.swf,expressInstall.swf
*/
var Cloopen = (function() {
	var DEBUG = false;//是否调试状态
	// var URL="http://127.0.0.1/website/";//swf的路径，本地环境
	var URL="http://www.cloopen.com/js/voip/";//swf的路径，生产环境
	var swfIsOK = false;//swf是否ok
	var _notifyCallBack = null;//回调函数
	var objSwf = null;//swf对象 
	var _userToken = null;//用户对应的token 
	var _currentState='IDLE';//场景当前状态:IDLE=空闲,ACTIVE=活动,CONNECTING=连接中,CONNECTED=已连接,OUTBOUND=呼出,INBOUND=呼入
	var srv_type='sandbox';//aliyun 负载均衡，沙盒环境 

	function showLog(msg) { //输出日志
		var startTime = new Date().getTime();
		if(DEBUG) $('body').append('<div>Cloopen___' + startTime + '___' + msg + '</div>');
	}

	function init(swfId, initCallBack, notifyCallBack, userToken) { //初始化：swf对象id,定义回调函数
		_notifyCallBack = notifyCallBack; //定义回调函数
		_userToken = userToken;//用户对应的token 
		swfIsOK = false;
		swfobject.embedSWF(URL+"webcalljs11.swf?timestamp=" + new Date(), swfId, "215", "138", "11.0.0", "expressInstall.swf", false, {
			allowScriptAccess: 'always'
		}, null, function(e) {
			//if(!objSwf) objSwf=swfobject.getObjectById("idvideophone");
			if(e.success) {
				objSwf = e.ref; //获取swf对象
				swfIsOK = true;//swf可以使用
				if(typeof initCallBack === "function") {
					Cloopen.showLog('Cloopen.initCallBack()--初始化成功后，连接服务器');
					initCallBack();
				}
			}
			showLog("e.success = " + e.success + "\ne.id = " + e.id + "\ne.ref = " + e.ref + "\n ====" + objSwf);
			return e.success;
		});
		return swfIsOK;
	}

	function connectplusByToken(userToken) { //连接登陆服务器
		_userToken = userToken;
		showLog("Cloopen.connectplus(.."+_userToken+"...)____"+swfIsOK);
		if(swfIsOK){
			_currentState='CONNECTING';//CONNECTING=连接中
			objSwf.connectplus('TOKEN', srv_type, _userToken, '');
			showLog("Cloopen.connectplus('TOKEN',srv_type," + _userToken + "','')");
		}else
			showLog('Cloopen:SWF is not ok');
	}

	function connectplus() { //连接登陆服务器
		connectplusByToken(_userToken);
	}

	function setMediaCodes(codes) { //设置音频格式
		if(swfIsOK){
			objSwf.setMediaCodes(codes);
			showLog('Cloope.setMediaCodes('+codes+')');
		}else
			showLog('Cloopen:SWF is not ok');
	}

	function disconnect() { //断开服务器连接
		if(swfIsOK){
			objSwf.disconnect();
			showLog('Cloope.disconnect');
		}else
			showLog('Cloopen:SWF is not ok');
	}

	function inviteplus(target) { //呼叫target
		if(swfIsOK){
			objSwf.inviteplus('TOKEN', target);
			showLog('Cloopen.inviteplus ' + target);
		}else
			showLog('Cloopen:SWF is not ok');
	}

	function invitetel(tel) { //呼叫target
		if(swfIsOK){
			objSwf.inviteplus('TEL', tel);
			showLog('Cloopen.invitetel  ' + tel);
		}else
			showLog('Cloopen:SWF is not ok');
	}

	function bye() { //断开通话
		if(swfIsOK){
			objSwf.bye();
			showLog('Cloopen.bye');
		}else
			showLog('Cloopen:SWF is not ok');
	}

	function accept() { //接受呼入
		if(swfIsOK){
			objSwf.accept();
			showLog('Cloopen.accept');
		}else
			showLog('Cloopen:SWF is not ok');
	}

	function reject() { //拒绝呼入
		if(swfIsOK){
			objSwf.reject('reject');//拒绝原因
			showLog('Cloopen.reject("reject")');
			/************write by liminglei start******************/
			
			//a();
			/************write by liminglei end******************/
		}else
			showLog('Cloopen:SWF is not ok');
	}

	function flexCallJsBack(doFun, msg) { //flex调用js的函数，名称固定
		switch (doFun)
		{
		case 'Microphone.Muted'://麦克风被禁止访问，扩大swf面积，现实隐私设置弹窗
			swfIsOK=false;
		  	objSwf.width=215;
		  	objSwf.height=138;
		  	//objSwf.securityshowsettings();
		  	showLog('Cloopen:麦克风被禁止访问，扩大swf面积，展示隐私设置弹窗');
		  	break;
		case 'Microphone.Unmuted'://麦克风被允许访问，缩小swf面积
			swfIsOK=true;
		  	objSwf.width=1;
		  	objSwf.height=1;
		  	showLog('Cloopen:麦克风被允许访问，缩小swf面积');
		  	_whenCallBack(funArry.idle);
		  	if (_currentState=='IDLE'){
				showLog('Cloopen:连接服务器，注册用户...');
				setMediaCodes('pcmu pcma');//设置音频格式
		  		connectplus();//连接注册
			}
		  	break;
		case 'idle'://恢复未连接初始状态，显示连接注册按钮
			_whenCallBack(funArry.idle);
		  	break;
		case 'connecting'://正在连接服务器注册，显示取消（断开）连接按钮
			_whenCallBack(funArry.connecting);
		  	break;
		case 'connected'://已经注册登录，显示呼叫按钮
			_whenCallBack(funArry.connected);
		  	break;
		case 'outbound'://正在呼出，显示取消按钮
			_whenCallBack(funArry.outbound);
		  	break;
		case 'inbound'://有呼入，显示接听+拒绝按钮
			_whenCallBack(funArry.inbound);
		  	break;
		case 'active'://通话中，显示挂机按钮
			_whenCallBack(funArry.active);
		  	break;
		default:
			;	
		}
		if(typeof _notifyCallBack === "function") {
			Cloopen.showLog('Cloopen.flexCallJsBack._notifyCallBack(' + doFun + '---' + msg + ')');
			_notifyCallBack(doFun, msg);
		}
	}
	function _whenCallBack(callback){//回调函数
		if(typeof callback === "function") callback();
	}
 
	var funArry={idle:null,
			connecting:null,
			connected:null,
			outbound:null,
			inbound:null,
			active:null
		};

	var exports = {
		init: init,
		debug: function() {
			DEBUG = true;
		},
		url:function(address){
			URL=address;
		},
                srvtype:function(st){
                        srv_type=st;
                },
		showLog: showLog,
		//////////////////////
		'connectplus': connectplus,
		setMediaCodes: setMediaCodes,
		disconnect: disconnect,
		inviteplus: inviteplus,
		invitetel: invitetel,
		bye: bye,
		accept: accept,
		reject: reject,
		//////////通话状态+动作/////reply///////
		when_idle:function(callback){funArry.idle = callback},//恢复未连接初始状态，显示连接注册按钮
		when_connecting:function(callback){funArry.connecting = callback},//正在连接服务器注册，显示取消（断开）连接按钮
		when_connected:function(callback){funArry.connected = callback},//已经注册登录，显示呼叫按钮
		when_outbound:function(callback){funArry.outbound = callback},//正在呼出，显示取消按钮
		when_inbound:function(callback){funArry.inbound = callback},//有呼入，显示接听+拒绝按钮
		when_active:function(callback){funArry.active = callback},//通话中，显示挂机按钮
		////////////////////
		flexCallJsBack: flexCallJsBack

	};
	return exports;
})();
